package com.shangze.test.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.shangze.test.R;
import com.shangze.test.base.BaseActivity;
import com.shangze.test.presenter.activity.MainPresenter;
import com.shangze.test.ui.fragment.CircleFriendsFragment;
import com.shangze.test.ui.fragment.HomeFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseActivity<MainPresenter> implements BottomNavigationBar.OnTabSelectedListener {


    @BindView(R.id.mframe)
    FrameLayout mframe;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    //数据源的集合
    private ArrayList<Fragment> list;
    HomeFragment homeFragment1;
    CircleFriendsFragment homeFragment2;
    HomeFragment homeFragment3;
    HomeFragment homeFragment4;
    int tag = 0;

    @Override
    public void initView() {
        initDatas();
        initEvents();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }


    private void initDatas() {

        //要先设计模式后再添加图标！
        bottomNavigationBar //值得一提，模式跟背景的设置都要在添加tab前面，不然不会有效果。
                .setActiveColor(R.color.white)//背景色
                .setInActiveColor("#999999")//默认未选择颜色
                .setBarBackgroundColor(R.color.toolbar_background);//set background color for navigation bar，设置底部导航栏颜色
        //设置按钮模式  MODE_FIXED表示固定   MODE_SHIFTING表示转移

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        //设置背景风格       BACKGROUND_STYLE_STATIC表示静态的  ，BACKGROUND_STYLE_RIPPLE表示涟漪的，也就是可以变化的 ，跟随setActiveColorResource里面d颜色变化
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setAutoHideEnabled(true);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "朋友圈"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "联系人"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "我的"))
                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();//所有的设置需在调用该方法前完成
        //设置选中第一个添加的按钮
        //bottomNavigationBar.selectTab(0, true);
        list = getFragments();
        setDefaultFragment();

    }


    /**
     * 加载Fragemnt
     */
    private ArrayList<Fragment> getFragments() {
        homeFragment1 = HomeFragment.newInstance();
        homeFragment2 = CircleFriendsFragment.newInstance();
        homeFragment3 = HomeFragment.newInstance();
        homeFragment4 = HomeFragment.newInstance();
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(homeFragment1);
        fragments.add(homeFragment2);
        fragments.add(homeFragment3);
        fragments.add(homeFragment4);
        return fragments;
    }

    /**
     * 设置默认的Item
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.mframe, homeFragment1);
        transaction.commit();
    }


    private void initEvents() {
        bottomNavigationBar //设置lab点击事件
                .setTabSelectedListener(this);
    }

    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter();
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onTabSelected(int position) {
        if (list != null) {
            if (position < list.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                //当前的fragment
                Fragment from = fm.findFragmentById(R.id.mframe);

                //点击即将跳转的fragment
                Fragment fragment = list.get(position);
                Fragment tagFrag = list.get(tag);
                if (fragment.isAdded()) {
                    // 隐藏当前的fragment，显示下一个
                    ft.hide(tagFrag).show(fragment);
                } else {
                    // 隐藏当前的fragment，add下一个到Activity中
                    ft.hide(tagFrag).add(R.id.mframe, fragment);
                }
                ft.commitAllowingStateLoss();
                tag = position;
            }
        }
    }

    @Override
    public void onTabUnselected(int position) {

        //这儿也要操作隐藏，否则Fragment会重叠
        if (list != null) {
            if (position < list.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = list.get(position);
                // 隐藏当前的fragment
                ft.hide(fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;

        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void onTabReselected(int position) {

    }
}
